package com.wanqing.community.util;

import java.io.Serializable;

/**
 * @author GangQiangGao
 *         <p>
 *         Description:查询条件解析类，生成查询语句的查询条件
 *         </p>
 * @version
 */
public class ConditionParse implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private StringBuffer condition = new StringBuffer();

	/**
	 *
	 *
	 */
	public ConditionParse() {

	}

	/**
	 * 根据条件字段、运算符和条件值拼凑查询条件
	 *
	 * @param fieldName
	 *            字段名称
	 * @param oper
	 *            关系运算符
	 * @param conditionValue
	 *            输入的查询条件值
	 */
	public void addCondition(String fieldName, String oper,
			String conditionValue) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			String tempOper = oper.trim().toUpperCase();
			if (tempOper.indexOf("LIKE") != -1) {
				condition.append(" AND ").append(fieldName).append(" ").append(
						oper).append(" ");
				condition.append("'%").append(conditionValue).append("%'");
			} else {
				condition.append(" AND ").append(fieldName).append(" ").append(
						oper).append(" ");
				condition.append("'").append(conditionValue).append("'");
			}
		}
	}
	
	//ly 2014-09-29
	public void addConditionStr(String str)
	{
		condition.append(" AND ").append(" ( ").append(str).append(" ) ");
	}

	/**
	 * 根据条件字段、运算符和条件值拼凑查询条件
	 *
	 * @param fieldName
	 *            字段名称
	 * @param oper
	 *            关系运算符
	 * @param direct
	 *            表示进行前模糊（front）、后模糊(back)、全模糊(all)
	 * @param conditionValue
	 *            输入的查询条件值
	 */
	public void addCondition(String fieldName, String oper,
			String conditionValue, String direct) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			String tempOper = oper.trim().toUpperCase();
			if (tempOper.indexOf("LIKE") != -1) {
				condition.append(" AND ").append(fieldName).append(" ").append(
						oper).append(" ");
			}
			if (tempOper.indexOf(">") != -1) {
				condition.append(" AND ").append(fieldName).append(" ").append(
						oper).append(conditionValue);
			} else {
				if (direct.equals("all") || direct.equals("front")) {
					condition.append("'%").append(conditionValue);
				} else {
					condition.append("'").append(conditionValue);
				}

				if (direct.equals("all") || direct.equals("back")) {
					condition.append("%'");
				} else {
					condition.append("'");
				}
			}
		}
	}

	/**
	 * 根据条件字段、运算符和条件值拼凑查询条件
	 *
	 * @param fieldName
	 *            字段名称
	 * @param oper
	 *            关系运算符
	 * @param conditionValue
	 *            输入的查询条件值
	 */
	public void addConditionIn(String fieldName, String oper,
			String conditionValue) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			String tempOper = oper.trim().toUpperCase();
			if (tempOper.indexOf("IN") != -1) {
				condition.append("  and ").append(fieldName).append(" ").append(oper)
						.append(" ").append(" (").append(conditionValue).append(") ");
			}
		}
	}
	
	/**
	 * @author 高刚强
	 * @version 2010-2-20
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addInCondition(String fieldName,String oper,int[] conditionValue){		
		if(null!=conditionValue && conditionValue.length>0){
			String tempOper = oper.trim().toUpperCase();
			if(tempOper.indexOf("IN")!=-1){
				StringBuilder sb = new StringBuilder();
				condition.append(" AND ").append(fieldName).append("  ").append(oper).append(" (");
				for (int value : conditionValue) {
					sb.append(value).append(",");
				}
				condition.append(StringTools.removeLastString(sb.toString(), ","));
				condition.append(")");
			}
		}
	}
	
	/**
	 * 增加字符串条件字段 conditionValue可以有多个值 用逗号分开，并且每个值为非字符串类型
	 *
	 * @version 2010-2-20
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addInCondition(String fieldName,String oper,String conditionValue){
		if(!StringTools.isEmptyOrNull(conditionValue)){
			String tempOper = oper.trim().toUpperCase();
			if(tempOper.indexOf("IN")!=-1){
				StringBuilder sb = new StringBuilder();
				condition.append(" AND ").append(fieldName).append("  ").append(oper).append(" (");
				String[] temp = conditionValue.split(",");
				for (String value : temp) {
					sb.append(value).append(",");
				}
				condition.append(StringTools.removeLastString(sb.toString(), ","));
				condition.append(")");
			}
		}
	}
	
	/**
	 * 增加字符串条件字段 conditionValue可以有多个值 用逗号分开，并且每个值为非字符串类型
	 *
	 * @version 2010-2-20
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addInCondition(String fieldName,String oper,String conditionValue,Class<?> requiredType){
		if(!StringTools.isEmptyOrNull(conditionValue)){
			String tempOper = oper.trim().toUpperCase();
			if(tempOper.indexOf("IN")!=-1){
				String[] tempValues = StringTools.split(conditionValue, ",");
				addInCondition(fieldName,oper,tempValues,requiredType);
			}
		}
	}
	
	/**
	 * 增加字符串条件字段 conditionValue为字符串数组
	 *
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addInCondition(String fieldName,String oper,String[] conditionValue,Class<?> requiredType){
		if(null!=conditionValue && conditionValue.length>0){
			String tempOper = oper.trim().toUpperCase();
			if(tempOper.indexOf("IN")!=-1){
				if(String.class.equals(requiredType)){
					this.addInCondition(fieldName, oper, conditionValue);
				}else if(Integer.class.equals(requiredType)){
					int[] temps = new int[conditionValue.length];
					for (int i = 0; i < conditionValue.length; i++) {
						String value = conditionValue[i];
						temps[i] = NumberDealTools.stringTranceInt(value);						
					}
					this.addInCondition(fieldName, oper, temps);
				}
			}
		}
	}
	
	/**
	 * 增加字符串条件字段，传入数组参数使用IN操作符
	 * @author GangQiangGao
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addInCondition(String fieldName,String oper,String[] conditionValue){
		if(null!=conditionValue && conditionValue.length>0){
			String tempOper = oper.trim().toUpperCase();
			if(tempOper.indexOf("IN")!=-1){
				StringBuilder sb = new StringBuilder();
				condition.append(" AND ").append(fieldName).append("  ").append(oper).append(" (");				
				for (String value : conditionValue) {
					sb.append("'").append(value).append("',");
				}
				condition.append(StringTools.removeLastString(sb.toString(), ","));
				condition.append(")");
			}
		}
	}

	/**
	 * Description: <br>
	 * 指定增加条件语句 <br>
	 * Implement: <br>
	 *
	 * @param conditionValue
	 */
	public void addCondition(String conditionValue) {
		condition.append(" ").append(conditionValue);
	}

	/**
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addCondition(String fieldName, String oper, int conditionValue) {
		String tempValue = String.valueOf(conditionValue);		
		if (!StringTools.isEmptyOrNull(tempValue)) {
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(" ").append(tempValue).append(" ");
		}
	}
	
	/**
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addCondition(String fieldName, String oper, Integer conditionValue) {
		String tempValue = StringTools.objectToString(conditionValue);		
		if (!StringTools.isEmptyOrNull(tempValue)) {
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(" ").append(tempValue).append(" ");
		}
	}
	
	/**
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addCondition(String fieldName, String oper, Double conditionValue) {
		String tempValue = StringTools.objectToString(conditionValue);		
		if (!StringTools.isEmptyOrNull(tempValue)) {
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(" ").append(tempValue).append(" ");
		}
	}
	
	

	/**
	 * 增加字符串条件字段 conditionValue可以有多个值 用逗号分开
	 *
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addStringCondition(String fieldName, String oper,
			String conditionValue) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			if (!conditionValue.contains(",")) {
				condition.append(" AND ").append(fieldName).append(" ").append(
						oper).append(" '").append(conditionValue).append("' ");
			} else {
				String[] temp = conditionValue.split(",");
				for (int i = 0; i < temp.length; i++) {
					String tempValue = temp[i];
					if (i == 0) {
						condition.append(" AND (").append(fieldName)
								.append(" ").append(oper).append(" '").append(
										tempValue).append("' ");
					} else {
						condition.append(" OR ").append(fieldName).append(" ")
								.append(oper).append(" '").append(tempValue)
								.append("' ");
					}
				}
				condition.append(")");
			}
		}
	}

	public void addIntsCondition(String fieldName, String oper,
			String conditionValue) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			if (!conditionValue.contains(",")) {
				condition.append(" AND ").append(fieldName).append(" ").append(
						oper).append(
						NumberDealTools.stringTranceInt(conditionValue));
			} else {
				String[] temp = conditionValue.split(",");
				for (int i = 0; i < temp.length; i++) {
					int tempValue = NumberDealTools.stringTranceInt(temp[i]);
					if (i == 0) {
						condition.append(" AND (").append(fieldName)
								.append(" ").append(oper).append(tempValue);
					} else {
						condition.append(" AND ").append(fieldName).append(" ")
								.append(oper).append(tempValue);
					}
				}
				condition.append(")");
			}
		}
	}

	/**
	 * Description:增加整型条件字段 <br>
	 * [参数列表，说明每个参数用途]
	 *
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 *            void
	 */
	public void addIntCondition(String fieldName, String oper,
			String conditionValue) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			int tempValue = Integer.parseInt(conditionValue);
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(tempValue);
		}

	}

	/**
	 * Description:增加float条件字段
	 *
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addFloatCondition(String fieldName, String oper,
			String conditionValue) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			float tempValue = Float.parseFloat(conditionValue);
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(tempValue);
		}

	}

	/**
	 * Description:增加浮点条件字段
	 *
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addDoubleCondition(String fieldName, String oper,
			String conditionValue) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			//double tempValue = Double.parseDouble(conditionValue);
			double tempValue = NumberDealTools.stringTranceDouble(conditionValue);
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(tempValue);
		}

	}

	/**
	 * Description:增加浮点条件字段
	 *
	 * @param fieldName
	 * @param oper
	 * @param conditionValue
	 */
	public void addDoubleCondition(String fieldName, String oper,
			double conditionValue) {
		condition.append(" AND ").append(fieldName).append(" ").append(oper)
				.append(" ").append(conditionValue).append(" ");
	}

	/**
	 * 增加日期条件类型，日期条件类型要经过转换
	 *
	 * @param fieldName
	 *            字段名称
	 * @param oper
	 *            关系运算符
	 * @param conditionValue
	 *            输入的日期值，格式必须是"yyyy-mm-dd hh:MM:ss"类型
	 */
	public void addDateCondition(String fieldName, String oper,
			String conditionValue) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			String dateValue = "to_date('" + conditionValue
					+ "','yyyy-mm-dd hh24:mi:ss')";
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(" ").append(dateValue);
		}
	}

	/**
	 * 增加日期条件类型，日期条件类型要经过转换
	 *
	 * @param fieldName
	 *            字段名称
	 * @param oper
	 *            关系运算符
	 * @param conditionValue
	 *            输入的日期值，格式必须是"yyyy-mm-dd"类型
	 */
	public void addDateCondition1(String fieldName, String oper,
			String conditionValue) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			String dateValue = "to_date('" + conditionValue + "','yyyy-mm-dd')";
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(" ").append(dateValue);
		}
	}
	
	/**
	 * <p>Description:增加日期条件类型，日期条件类型要经过转换<p>
	 * <p>modifiContent:<p>
	 * @param fieldName 字段名称
	 * @param oper 关系运算符
	 * @param conditionValue 输入的日期值
	 * @param format 输入的日期值的格式
	 */
	public void addDateCondition(String fieldName, String oper,String conditionValue,String format){
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			String dateValue = "to_date('" + conditionValue + "','"+format+"')";
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(" ").append(dateValue);
		}
	}

	/**
	 * 根据条件字段、运算符和条件值拼凑查询条件
	 *
	 * @param fieldName
	 *            字段名称
	 * @param oper
	 *            关系运算符
	 * @param conditionValue
	 *            输入的查询条件值
	 */
	public void addNewCondition(String fieldName, String oper,
			String conditionValue) {
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(" ");
			condition.append(conditionValue).append(" ");

		}
	}

	/**
	 * 增加WHERE字符串在前面，有些情况下原查询语句没有条件语句， 则要在条件语句前增加"WHERE"开头。 conditionValue
	 */
	public void addWhereStr() {
		// condition.insert(0, " WHERE 1=1 ");
		// 2010年4月30日修改以为下情况
		if (condition.length() > 0) {
			String temp = condition.toString().toLowerCase();
			int skip = temp.indexOf("and");
			condition.delete(0, skip + 3);
			condition.insert(0, " WHERE ");
		} else {
			condition.insert(0, " WHERE 1=1 ");
		}
	}

	/**
	 * 多表联合查询
	 *
	 * @param conditionValue
	 */
	public void addUnionStr(String conditionValue) {
		condition.insert(0, conditionValue);
	}

	public String getCondition() {
		return condition.toString();
	}

	public void setCondition(String condition) {
		this.condition.setLength(0);
		this.condition.append(condition);
	}	
	
	public void addConditionNullOrNotNull(String fieldName, String oper,String conditionValue){
		condition.append("  AND ").append(fieldName).append(" ").append(oper).append(" ").append(conditionValue).append(" ");
	}
	/**
	 * <p>Description:移出所有的条件<p>
	 * @modifier 
	 * @modifiDate 
	 * <p>modifiContent:<p>
	 */
	public void deleteAll(){
		int length = this.condition.length();
		if(length>0){
			this.condition.delete(0, length);
		}
	}
	
	public void addMySqlDate(String fieldName, String oper,String conditionValue){
		if (!StringTools.isEmptyOrNull(conditionValue)) {
			String dateValue = "str_to_date('" + conditionValue + "','%Y-%m-%d')";
			condition.append(" AND ").append(fieldName).append(" ")
					.append(oper).append(" ").append(dateValue);
		}
	}
}
